using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UMC.Web;
using UMC.Web.UI;

using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow
{


    [Apiumc("WorkFlow", "Log", Auth = WebAuthType.Guest, Desc = "流程审批日志")]
    public class WorkFlowLogActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var SId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
            {
                this.Prompt("无设置流程");
                return this.DialogValue("none");
            })) ?? 0;
            if (SId == 0)
            {
                return;
            }
            var Shape = this.AsyncDialog("Shape", g =>
            {
                return this.DialogValue("ALL");
            });
            if (Shape == "ALL")
            {
                Shape = null;
            }
            var task = TaskManager.Instance(SId);
            if (task == null)
            {
                return;
            }
            var form = request.SendValues ?? new UMC.Web.WebMeta();
            if (form.ContainsKey("limit") == false)
            {

                this.Context.Send(new UISectionBuilder(request.Model, request.Command, new WebMeta(request.Arguments.GetDictionary()))
                        .Builder(), true);
            }

            var logs = DataFactory.Instance().TaskLog(SId, Shape).OrderByDescending(r => r.CreateTime);// new List<TaskLog>();
          

            var ui = UISection.Create(new UITitle(task.Text));

            if (String.IsNullOrEmpty(Shape) == false)
            {
                var shape = task.Shapes.Find(s => s.Id == Shape);

                ui.AddCell('\uf0e8', "流程结点", String.Format("{1}({0})", shape.Id, shape.Text));

                if (shape.Status == ShapeStatus.Activation)
                {
                    ui.AddCell('\uf007', "当前审批人", shape.Identity.Alias, new UIClick("Account", "Staff", shape.Identity.Username));
                }
            }
            else
            {
                var status = "准备中";
                switch (task.Status)
                {
                    case TaskStatus.Completed:
                        status = "已完结";
                        break;
                    case TaskStatus.Locked:
                        status = "被锁定";
                        break;
                    case TaskStatus.InReview:
                        status = "审批中";
                        break;
                    case TaskStatus.Prepare:
                        status = "准备中";
                        break;
                    case TaskStatus.Termination:
                        status = "已终止";
                        break;
                }
                ui.AddCell('\uf0e8', "流程状态", status);

            }



            if (logs.Count() > 0)
            {
                var se = ui.NewSection();
                foreach (var dr in logs)
                {
                    var data = new WebMeta();
                    data["icon"] = "\uf007";
                    data["ticon"] = "\uf073";
                    data["time"] = String.Format("{0:yyyy.MM.dd HH:mm}", dr.CreateTime);
                    data["title"] = dr.Explain;
                    var cell = new UICMS(new UIClick("Account", "Staff", dr.Username), data);
                    var style = cell.Style.Name("tag");
                    var Str = "保存";
                    switch (dr.CommandType)
                    {
                        case CommandType.Apostille:
                            Str = "加签";
                            break;
                        case CommandType.Approval:
                            Str = "通过";
                            break;
                        case CommandType.Lock:
                            Str = "锁定";
                            break;
                        case CommandType.Reject:
                            Str = "驳回";
                            break;
                        case CommandType.Submit:
                            Str = "申请";
                            break;
                        case CommandType.Terminate:
                            Str = "终止";
                            break;
                    }

                    cell.Tag(Str);
                    data["name"] = dr.Alias;


                    cell.Left("{icon} {name}");
                    cell.Right("{ticon} {time}");
                    cell.Style.Name("icon").Font("wdk").Size(12);
                    cell.Style.Name("ticon").Font("wdk").Size(12);
                    cell.Style.Name("desc").Color(0x999);
                    se.Add(cell);
                }
            }
            else
            {
                ui.NewSection().Add("Desc", new UMC.Web.WebMeta().Put("desc", "未有审批日志").Put("icon", "\uEA05")
                    , new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}")
                    , new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));//.Name 

            }


            response.Redirect(ui);
        }

    }

}